﻿using MyDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rtz2Xlsx
{
    public partial class FormSettings : Form
    {
        public static ComboBox comboBoxPlanSelection_Static = null;
        public static string svIndicator = "*";
        public FormSettings()
        {
            InitializeComponent();            
        }
        private void FormSettings_Load(object sender, EventArgs e)
        {
            comboBoxPlanSelection_Static = comboBoxPlanSelection;
            LoadXml();
            UpdateComboBoxPlanSelection();
        }
        private void LoadXml()
        {
            textBoxPlanName.Text = FormMain.Attribute_Parent_Plus_NodeNames_Children_StrList[0][0];
            textBoxHeaders.Text = FormMain.Attribute_Parent_Plus_NodeNames_Children_StrList[1][0];
            textBoxBytesArr.Text = FormMain.Attribute_Parent_Plus_NodeNames_Children_StrList[1][1];
            textBoxEncodingStr.Text = FormMain.Attribute_Parent_Plus_NodeNames_Children_StrList[1][2];
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonManagePlans_Click(object sender, EventArgs e)
        {
            FormManageSettingsPlans frmMngPlns = new FormManageSettingsPlans();
            if (!FormMain.IsFormOpen(frmMngPlns))
            {
                frmMngPlns.Show();
            }
            else
            {
                frmMngPlns.BringToFront();
            }
        }

        //保存方案
        private void buttonSave_Click(object sender, EventArgs e)
        {
            //验证文本框信息是否合法
            if (VarifyXmlInput())
            {
                FormMain.xpath_ToPlan = FormMain.xpath_Plan + "[@" + XmlSettings.planAttribute_1 + "='" + textBoxPlanName.Text + "']";
                string[] strArr_Attributes = new string[3];
                strArr_Attributes[0] = textBoxPlanName.Text;    //获取方案名称                
                strArr_Attributes[2] = DateTime.Now.ToString(); //获取修改时间：当前时间
                string[] strArr_Nodes = new string[3] { textBoxHeaders.Text, textBoxBytesArr.Text, textBoxEncodingStr.Text };

                //判断方案是否为新增
                if (FormMain.plans.Contains(textBoxPlanName.Text))
                {
                    //保存
                    strArr_Attributes[1] = FormMain.Attribute_Parent_Plus_NodeNames_Children_StrList[0][1]; //获取创建时间
                    FormMain.xML_Commons.UpdateXmlAttributes_And_ChildrenNodesByName_ToParent(FormMain.xpath_ToPlan, strArr_Attributes, XmlSettings.Attributes_Parent, strArr_Nodes, XmlSettings.NodeNames_Children);
                }
                else
                {
                    //另存为
                    strArr_Attributes[1] = DateTime.Now.ToString(); //获取创建时间
                    FormMain.xML_Commons.AppendChild_CreateAttributes_CreateGrandChildrenNodes("/" + XmlSettings.settings, XmlSettings.plan, XmlSettings.Attributes_Parent, strArr_Attributes, XmlSettings.NodeNames_Children, strArr_Nodes);
                    FormMain.planSelected = textBoxPlanName.Text;
                }
                RefreshXmlInfo(textBoxPlanName.Text);
                FormMain.UpdateComboBoxSettingsPlans_FromOtherForms();
                UpdateComboBoxPlanSelection();
                this.Text = this.Text.Replace(svIndicator, string.Empty);
            }
        }

        private bool VarifyXmlInput()
        {
            bool reVal = true;

            //检验是否存在空值
            if (textBoxPlanName.Text == string.Empty)
            {
                reVal = false;
                MessageBox.Show("存在空文本框！");
                return reVal;
            }
            else if (textBoxHeaders.Text == string.Empty)
            {
                reVal = false;
                MessageBox.Show("存在空文本框！");
                return reVal;
            }
            else if (textBoxBytesArr.Text == string.Empty)
            {
                reVal = false;
                MessageBox.Show("存在空文本框！");
                return reVal;
            }
            else if (textBoxEncodingStr.Text == string.Empty)
            {
                reVal = false;
                MessageBox.Show("存在空文本框！");
                return reVal;                
            }

            string[] headers = textBoxHeaders.Text.Split(new string[] { "||", "," }, StringSplitOptions.RemoveEmptyEntries);
            //检查headers和byteArr数量是否相等
            if (headers.Length != textBoxBytesArr.Text.Split(new string[] { "||", "," }, StringSplitOptions.RemoveEmptyEntries).Length)
            {
                reVal = false;
                MessageBox.Show("各列标题数量和byte分割数组元素数量不一致！");
                return reVal;                
            }

            //检查headers中是否有重复字段
            if (headers.Length != headers.Distinct().Count())
            {
                MessageBox.Show("标题存在重复字段！");
                reVal = false;
            }

            return reVal;
        }

        //更新comboBoxPlanSelection
        public static void UpdateComboBoxPlanSelection()
        {
            comboBoxPlanSelection_Static.Items.Clear();            
            for (int i = 0; i < FormMain.plans.Length; i++)
            {
                comboBoxPlanSelection_Static.Items.Add(FormMain.plans[i]);
            }
            comboBoxPlanSelection_Static.Items.Add("新建方案");
            comboBoxPlanSelection_Static.Text = FormMain.planSelected;
        }

        //从FormPlanManagement更新comboBoxPlanSelection
        public static void UpdateComboBoxPlanSelection_FromFormPlanManagement()
        {
            comboBoxPlanSelection_Static.Items.Clear();
            RefreshXmlInfo(FormMain.plans[0]);
            for (int i = 0; i < FormMain.plans.Length; i++)
            {
                comboBoxPlanSelection_Static.Items.Add(FormMain.plans[i]);
            }
            comboBoxPlanSelection_Static.Items.Add("新建方案");
            comboBoxPlanSelection_Static.Text = FormMain.planSelected;
        }

        private void comboBoxPlanSelection_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comboBoxPlanSelection.SelectedItem.ToString() != "新建方案")
            {
                RefreshXmlInfo(comboBoxPlanSelection.Text);
                LoadXml();
            }
            else
            {
                textBoxPlanName.Text = "";
                textBoxHeaders.Text = "";
                textBoxBytesArr.Text = "";
                textBoxEncodingStr.Text = "GBK";
            }
        }
        public static void RefreshXmlInfo(string planSelected)
        {
            FormMain.planSelected = planSelected;
            FormMain.UpdateXmlInfo_ByAttribute_1(FormMain.planSelected);
            FormMain.comboBoxSettingsPlans_Static.SelectedItem = FormMain.planSelected;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBoxPlanName_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.Text != "转换设置" + svIndicator)
            {
                this.Text = "转换设置" + svIndicator;
            }
        }

        private void textBoxHeaders_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.Text != "转换设置" + svIndicator)
            {
                this.Text = "转换设置" + svIndicator;
            }
        }

        private void textBoxBytesArr_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.Text != "转换设置" + svIndicator)
            {
                this.Text = "转换设置" + svIndicator;
            }
        }

        private void textBoxEncodingStr_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.Text != "转换设置" + svIndicator)
            {
                this.Text = "转换设置" + svIndicator;
            }
        }

        private void FormSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Text.Contains(svIndicator))
            {
                if (DialogResult.OK == MessageBox.Show("设置尚未保存，确定要退出吗？", "关闭提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    this.FormClosing -= new FormClosingEventHandler(this.FormSettings_FormClosing);
                    //Application.Exit();  //退出进程
                }
                else
                {
                    e.Cancel = true;  //取消。返回窗体
                }
            }
        }

        private void buttonSpecAny1_Click(object sender, EventArgs e)
        {
            FrmByteSplitHelper frmByteSplitHelper = new FrmByteSplitHelper();
            frmByteSplitHelper.Show();
        }
    }
}
